package com.neusoft.controller;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Wallet;
import com.neusoft.po.WalletTransaction;
import com.neusoft.service.WalletService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 钱包控制器类，提供钱包相关的API接口。
 */
@RestController
@RequestMapping("/wallet")
@RefreshScope       //开启动态刷新
public class WalletController {

    @Autowired
    private WalletService walletService;

    /**
     * 获取用户的余额信息。
     *
     * @param userId 用户ID
     * @return 包含用户余额的钱包对象
     */
    @GetMapping("/getBalance/{userId}")
    public CommonResult<Wallet> getBalance(@PathVariable("userId") String userId) throws Exception {
        Wallet wallet = walletService.getWalletByUserId(userId);
        return new CommonResult<>(200, "success(10700)", wallet);
    }

    /**
     * 为用户钱包充值。
     *
     * @param userId 用户ID
     * @param amount 充值金额
     * @return 操作结果消息
     */
    @PostMapping("/recharge/{userId}/{amount}")
    public CommonResult<Boolean> recharge(@PathVariable("userId") String userId, @PathVariable("amount") Double amount) throws Exception {
        Boolean result = walletService.recharge(userId, amount);
        return new CommonResult<>(200, "success(10700)", result);
    }

    /**
     * 为用户钱包提现。
     *
     * @param userId 用户ID
     * @param amount 提现金额
     * @return 操作结果消息
     */
    @PostMapping("/withdraw/{userId}/{amount}")
    public CommonResult<Boolean> withdraw(@PathVariable("userId") String userId, @PathVariable("amount") Double amount) throws Exception {
        Boolean result = walletService.withdraw(userId, amount);
        return new CommonResult<>(200, "success(10700)", result);
    }

    /**
     * 用户之间进行转账。
     *
     * @param fromUserId 转出方用户ID
     * @param toUserId 接收方用户ID
     * @param amount 转账金额
     * @return 操作结果消息
     */
    @PostMapping("/transfer/{fromUserId}/{toUserId}/{amount}")
    public CommonResult<Boolean> transfer(@PathVariable("fromUserId") String fromUserId, @PathVariable("toUserId") String toUserId, @PathVariable("amount") Double amount) throws Exception {
        boolean result = walletService.transfer(fromUserId, toUserId, amount);
        return new CommonResult<>(200, "success(10700)", result);
    }

    /**
     * 获取用户的所有交易记录。
     *
     * @param userId 用户ID
     * @return 交易记录列表
     */
    @GetMapping("/getTransactions/{userId}")
    public CommonResult<List> getTransactions(@PathVariable("userId") String userId) throws Exception {
        List<WalletTransaction> list = walletService.getTransactionsByUserId(userId);
        return new CommonResult<>(200, "success(10700)", list);
    }

    /**
     * 确认收货接口。
     *
     * @param toUserId 收货方用户ID
     * @param amount 确认金额
     * @return 是否成功
     */
    @PostMapping("/confirmReceipt/{toUserId}/{amount}")
    public CommonResult<Boolean> confirmReceipt(@PathVariable("toUserId") String toUserId, @PathVariable("amount") Double amount) throws Exception {
        boolean result = walletService.confirmReceipt(toUserId, amount);
        return new CommonResult<>(200, "success(10700)", result);
    }
}
